using UnityEngine;
using System.Collections;

public class initialisationScript : MonoBehaviour {
    public bool debug = true;
    public string songsDirectory = "midi";
    public int numTrack = 0;
    public int nbNotes;
    public int firstNote = 0;
    public int maxNbNotes = 40;
    public bool onPartition = true;

    public AudioClip referenceAudioClip;
    public float referenceAudioClipFrequency = 1760; //frequency of A6
    public float a0Frequency = 27.5f;
    public float semitoneHeight = 100f;
    public int quarterNoteDuration = 96;
    public int defaultTempo = 120;
    public float relativeTempo = 1f;
    public int quarterNoteDistance = 230;
    public int quarterNoteSize = 75;
    public int relativeAngularVelocity = 300;
    public float minAngle = 20f;
    public float maxAngle = 160.0f;
    public int relativeMass = 1000;

    public bool isInitTime = false;
    public bool isLevelReady = false;
    public bool racing = false;

    public Texture2D sharpNoteTexture;
    
    // Use this for initialization
	void Awake () {

        print("Parameters initialization");

        GeneralParameters.debug = debug;
        GeneralParameters.songsDirectory = songsDirectory;
        GeneralParameters.numTrack = numTrack;
        GeneralParameters.firstNote = firstNote;
        GeneralParameters.maxNbNotes = maxNbNotes;
        GeneralParameters.onPartition = onPartition;

        GeneralParameters.referenceAudioClip = referenceAudioClip;
        GeneralParameters.referenceAudioClipFrequency = referenceAudioClipFrequency;
        GeneralParameters.a0Frequency = a0Frequency;
        GeneralParameters.semitoneHeight = semitoneHeight;
        GeneralParameters.defaultTempo = defaultTempo;
        GeneralParameters.relativeTempo = relativeTempo;
        GeneralParameters.quarterNoteDuration = quarterNoteDuration;
        GeneralParameters.quarterNoteDistance = quarterNoteDistance;
        GeneralParameters.quarterNoteSize = quarterNoteSize;
        GeneralParameters.relativeAngularVelocity = relativeAngularVelocity;
        GeneralParameters.minAngle = minAngle;
        GeneralParameters.maxAngle = maxAngle;
        GeneralParameters.relativeMass = relativeMass;

        GeneralParameters.sharpNoteTexture = sharpNoteTexture;

        GeneralParameters.isInitTime = isInitTime;
        GeneralParameters.isLevelReady = isLevelReady;

        GeneralParameters.setParameters();
    }
	
	// Update is called once per frame
	void Update () {
        GeneralParameters.debug = debug;

        nbNotes = GeneralParameters.nbNotes;
        numTrack = GeneralParameters.numTrack;
        onPartition = GeneralParameters.onPartition;
        isInitTime = GeneralParameters.isInitTime;
        isLevelReady = GeneralParameters.isLevelReady;

        if (isLevelReady && isInitTime)
            GeneralParameters.isInitTime = checkAllInitialized();
        else if (isLevelReady && !racing)
            takeOffForAll();
	}

    public bool checkAllInitialized()
    {
        bool notAllReady = true;
        foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player"))
        {
            shipControllerScript playerScript = player.GetComponent<shipControllerScript>();
            if (!playerScript.ready())
            {
                notAllReady = false;
                print("Player \"" + player.name + "\" is not ready yet");
            }
            else
                print("Player \"" + player.name + "\" is ready");
        }
        return !notAllReady;
    }

    public void prepareToTakeOff()
    {
        print("Prepare to take-off...");
        GeneralParameters.isInitTime = true;
    }

    public void takeOffForAll()
    {
        foreach (GameObject player in GameObject.FindGameObjectsWithTag("Player"))
            player.GetComponent<shipControllerScript>().takeOff();
        racing = true;
    }
}
